package com.sixteengroup.personnelms.controller.admin;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageInfo;
import com.sixteengroup.personnelms.bean.Performance;
import com.sixteengroup.personnelms.bean.User;
import com.sixteengroup.personnelms.service.admin.PerformanceService;
import com.sixteengroup.personnelms.util.JWTutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/performance")
public class PerformanceController {

    @Autowired
    PerformanceService performanceService;
    //获取列表
    @RequestMapping("/getList")
    public Map<String,Object> getList(@RequestBody Performance performance){
        Map<String,Object> map = new HashMap<>();
        PageInfo<Performance> pageInfo = performanceService.getList(performance);
        try {
            map.put("code",200);
            map.put("perList",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
        } catch (Exception e){
            map.put("code",500);
            map.put("msg","服务器错误！");
        }
        return map;
    }

    //获取考核人列表
    @RequestMapping("/getkhrs")
    public Map<String,Object> getkhrs(){
        Map<String,Object> map = new HashMap<>();
        try {
            List<User> khrs = performanceService.getKhrs();
            map.put("code",200);
            map.put("khrs",khrs);
        }catch (Exception e){
            map.put("code",500);
            map.put("msg","服务器错误！");
        }
        return map;
    }
    //获取被考核人列表
    @RequestMapping("/getbkhrs")
    public Map<String,Object> getbkhrs(){
        Map<String,Object> map = new HashMap<>();
        try {
            List<User> bkhrs = performanceService.getBkhrs();
            map.put("code",200);
            map.put("bkhrs",bkhrs);
        }catch (Exception e){
            map.put("code",500);
            map.put("msg","服务器错误！");
        }
        return map;
    }

    //保存绩效考核
    @RequestMapping("/save")
    public Map<String,Object> save(@RequestBody Performance  per, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        try {
            DecodedJWT token = JWTutil.getTokenInfo(request.getHeader("token"));
            Integer id = token.getClaim("id").asInt();
            per.setOperUser(String.valueOf(id));
            performanceService.save(per);
            map.put("code",200);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","服务器错误！");
        }
        return map;
    }

    //修改绩效考核
    @RequestMapping("/update")
    public Map<String,Object> update(@RequestBody Performance  per, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        try {
            DecodedJWT token = JWTutil.getTokenInfo(request.getHeader("token"));
            Integer id = token.getClaim("id").asInt();
            per.setOperUser(String.valueOf(id));
            performanceService.update(per);
            map.put("code",200);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","服务器错误！");
        }
        return map;
    }

    //删除
    @RequestMapping("/delete/{id}")
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            performanceService.delete(id);
            map.put("code",200);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","服务器错误！");
        }
        return map;
    }

    //获取要修改的绩效考核信息
    @RequestMapping("/getPer/{id}")
    public Map<String,Object> getUserById(@PathVariable("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            Performance performance = performanceService.getPerById(id);
            map.put("code",200);
            map.put("per",performance);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","服务器错误！");
        }
        return map;
    }

}
